package com.zhss.eshop.commodity.mapper;

import com.zhss.eshop.commodity.domain.model.CommodityCategory;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommodityCategoryMapper {
    int deleteByPrimaryKey(Long id);

    Long insertSelective(CommodityCategory record);

    CommodityCategory selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(CommodityCategory record);

    /**
     * 查询根类目
     *
     * @return 根类目集合
     */
    @Select("SELECT "
            + "id,"
            + "name,"
            + "description,"
            + "parent_id,"
            + "is_leaf,"
            + "gmt_create,"
            + "gmt_modified "
            + "FROM commodity_category "
            + "WHERE parent_id IS NULL")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "name", property = "name"),
            @Result(column = "description", property = "description"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "is_leaf", property = "leaf"),
            @Result(column = "gmt_create", property = "gmtCreate"),
            @Result(column = "gmt_modified", property = "gmtModified")
    })
    List<CommodityCategory> listRoots();

    /**
     * 查询子类目
     *
     * @param id 父类目id
     * @return 子类目集合
     */
    @Select("SELECT "
            + "id,"
            + "name,"
            + "description,"
            + "parent_id,"
            + "is_leaf,"
            + "gmt_create,"
            + "gmt_modified "
            + "FROM commodity_category "
            + "WHERE parent_id=#{id}")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "name", property = "name"),
            @Result(column = "description", property = "description"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "is_leaf", property = "leaf"),
            @Result(column = "gmt_create", property = "gmtCreate"),
            @Result(column = "gmt_modified", property = "gmtModified")
    })
    List<CommodityCategory> listChildren(@Param("id") Long id);
}
